//
//  ShareTool.m
//  Shengzhuli
//
//  Created by 卡忙科技 on 16/8/20.
//  Copyright © 2016年 kamangkeji. All rights reserved.
//

#import "ShareTool.h"
#import "ShowShareView.h"

#import <UMSocialCore/UMSocialCore.h>
#import "MBProgressHUD+MJ.h"
#import "SZLAccountHelper.h"
#import "ActivityNetWork.h"

static NSString *UMAPPKEY = @"58f5c3b17666134bba0007d7";

@interface ShareTool()

@property (nonatomic,weak) ShowShareView *shareView;
@property (nonatomic,strong) ShareContentModel *model;
@property (nonatomic,strong) NSString *activityId;
@end

@implementation ShareTool

static ShareTool *_tool;
+ (instancetype)shareTool
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (!_tool) {
            _tool = [[ShareTool alloc]init];
        }
    });
    return _tool;
}
+ (void)setAppkey
{
    [[UMSocialManager defaultManager]setUmSocialAppkey:UMAPPKEY];
//    [[UMSocialManager defaultManager] openLog:YES];
    [UMSocialGlobal shareInstance].isUsingHttpsWhenShareContent = NO;//关闭https验证
}

+ (void)share:(ShareContentModel *)model
{
    ShareTool *tool = [ShareTool shareTool];
    tool.model = model;
    [tool shareDetail];
    [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_WechatSession appKey:@"wx637ea06a1c6d1fff" appSecret:@"1b2n3v4fx5v56vj8f3hd5t7g5f6hj8fh" redirectURL:model.pageUrl];
    [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_QQ appKey:@"1105821097" appSecret:nil redirectURL:model.pageUrl];
    activityId = @"";
    activityId = tool.model.idd;
}
//直接调用分享 不弹出界面 1是微信聊天 2是朋友圈
+ (void)shareWithImg:(UIImage *)img type:(NSInteger)shareType
{
    [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_WechatSession appKey:@"wx637ea06a1c6d1fff" appSecret:@"1b2n3v4fx5v56vj8f3hd5t7g5f6hj8fh" redirectURL:nil];
    
    //如何修改toast“分享成功”&弹框的位置
    UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
    //创建图片内容对象
    UMShareImageObject *shareObject = [[UMShareImageObject alloc] init];
    //如果有缩略图，则设置缩略图
    shareObject.thumbImage = img;
    [shareObject setShareImage:img];
    
    //分享消息对象设置分享内容对象
    messageObject.shareObject = shareObject;
    
    UIViewController *vc = [UIApplication sharedApplication].keyWindow.rootViewController;
    UMSocialPlatformType type = -2;
    shareType = (ShareType)shareType;
    if (shareType == shareTypeWX) {
        type = UMSocialPlatformType_WechatSession;
    }else if(shareType == shareTypeFriends){
        type = UMSocialPlatformType_WechatTimeLine;
    }
    
    [[UMSocialManager defaultManager] shareToPlatform:type messageObject:messageObject currentViewController:vc completion:^(id result, NSError *error) {
        if (error) {
            UMSocialLogInfo(@"************Share fail with error %@*********",error);
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [MBProgressHUD showError:@"分享取消"];
            });
        }else{
            if ([result isKindOfClass:[UMSocialShareResponse class]]) {
                UMSocialShareResponse *resp = result;
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [MBProgressHUD showSuccess:@"分享成功"];
                });
            }else{
                UMSocialLogInfo(@"response data is %@",result);
            }
        }
    }];
}

- (void)shareDetail
{
    ShowShareView *shareView = [[ShowShareView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.shareView = shareView;
    [shareView showshareView:^(ShareType type) {
        [self showShareViewClickType:type];
    }];
}
- (void)showShareViewClickType:(ShareType)shareType
{
    [self.shareView animatedHide];
    //如何修改toast“分享成功”&弹框的位置
    UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
    UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:self.model.title descr:self.model.content thumImage:self.model.sharePicUrl];
    shareObject.webpageUrl = self.model.pageUrl;
    messageObject.shareObject = shareObject;
    
    UIViewController *vc = [UIApplication sharedApplication].keyWindow.rootViewController;
    
    UMSocialPlatformType type = -2;
    if (shareType == shareTypeWX) {
        type = UMSocialPlatformType_WechatSession;
    }else if(shareType == shareTypeFriends){
        type = UMSocialPlatformType_WechatTimeLine;
    }else{
        type = UMSocialPlatformType_QQ;
    }
    
    
    [[UMSocialManager defaultManager] shareToPlatform:type messageObject:messageObject currentViewController:vc completion:^(id result, NSError *error) {
        if (error) {
            UMSocialLogInfo(@"************Share fail with error %@*********",error);
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [MBProgressHUD showError:@"分享取消"];
            });
        }else{
            if ([result isKindOfClass:[UMSocialShareResponse class]]) {
                UMSocialShareResponse *resp = result;
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [MBProgressHUD showSuccess:@"分享成功"];
                });
            }else{
                UMSocialLogInfo(@"response data is %@",result);
            }
        }
    }];
}
@end
